<template>
  <div>
    <header-title :header-title="title" />
    <vehicle-form @on-success-valid="handleSubmit" />
    <el-button
      type="primary"
      style="margin-bottom: 10px"
      @click="handleClick"
    >
      新增车辆
    </el-button>
    <vehicle-dialog
      :row="row"
      :dialog-visible="visible"
      @cancel-dialog="handleCancelDialog"
      @confirm-dialog="handleConfirmDialog"
    />
    <confirm-dialog
      :dialog-visible="confirmVisible"
      @cancel-dialog="handleCancel"
      @confirm-dialog="handleConfirm"
    />
    <el-table
      v-loading="loading"
      :data="VehicleTableData()"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        type="index"
        :index="getIndex"
        label="序列"
        width="80"
        align="center"
      />
      <el-table-column
        prop="id"
        label="车辆编号"
        min-width="100"
        align="center"
      />
      <el-table-column
        prop="num"
        label="车牌号"
        min-width="120"
        align="center"
      />
      <el-table-column
        prop="type0"
        label="车辆品牌"
        min-width="130"
        align="center"
      />
      <el-table-column
        prop="type1"
        label="车辆类型"
        min-width="130"
        align="center"
      />
      <el-table-column
        prop="color"
        label="颜色"
        min-width="95"
        align="center"
      />
      <el-table-column
        label="首登日期"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <div>{{ scope.row.regdate ? scope.row.regdate.slice(0,-8) : '-' }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="年审日期"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <div>{{ scope.row.yearcare ? scope.row.yearcare.slice(0,-8) : '-' }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="交强险日期"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <div>{{ scope.row.yearinsure0 ? scope.row.yearinsure0.slice(0,-8) : '-' }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="商业险日期"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <div>{{ scope.row.yearinsure ? scope.row.yearinsure.slice(0,-8) : '-' }}</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="ctime"
        label="创建日期"
        min-width="170"
        align="center"
      />
      <el-table-column
        prop="city"
        label="所在地区"
        min-width="130"
        align="center"
      />
      <el-table-column
        label="车辆状态"
        min-width="80"
        align="center"
      >
        <template slot-scope="scope">
          <span>
            {{
              scope.row.state === 0 ? '下架' : scope.row.state === 1 ? '待租' : scope.row.state === 2 ? '待发车' : scope.row.state === 3 ? '待收车' : '-'
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        min-width="220"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            :disabled="scope.row.state === 1"
            size="mini"
            plain
            @click="handleOpen(scope.row)"
          >
            启用
          </el-button>
          <el-button
            type="primary"
            :disabled="scope.row.state === 0"
            size="mini"
            plain
            @click="handleClose(scope.row)"
          >
            暂停
          </el-button>
          <el-button
            size="mini"
            plain
            @click="handleClick(scope.row)"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="VehicleDataCount()"
      @click-page="handleClickPage"
      @change-pagesize="handleSizeChange"
    />
  </div>
</template>
<script>
import HeaderTitle from '@/components/HeaderTitle'
import VehicleForm from '@/components/VehicleForm'
import VehicleDialog from '@/components/VehicleDialog'
import ConfirmDialog from '@/components/ConfirmDialog'
import Pagination from '@/components/Pagination'
import mixin from '@/mixin/mixin'
export default {
  components: {
    HeaderTitle,
    VehicleForm,
    VehicleDialog,
    ConfirmDialog,
    Pagination
  },
  mixins: [mixin],
  data() {
    return {
      title: '车辆管理',
      page: 1,
      pagesize: 10,
      row: {},
      visible: false,
      confirmVisible: false,
      selectData: {},
      confirmData: {},
      loading: false
    }
  },
  mounted() {
    this.getVehicleData({
      page: this.page,
      rows: this.pagesize
    })
    this.getCarNameData({ type: 0 })
    this.getCarTypeData({ type: 1 })
  },
  methods: {
    getIndex(index) {
      return index + (this.page - 1) * this.pagesize + 1
    },
    handleSubmit(playload) {
      this.lodaing = true
      this.selectData = playload
      this.getVehicleData({
        page: this.page,
        rows: this.pagesize,
        ...playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.lodaing = false
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    handleClick(row) {
      let newRow = {}
      if (row.id) {
        newRow = { ...row }
      }
      this.row = newRow
      this.visible = true
    },
    handleOpen(row) {
      this.loading = true
      this.editVehicleData({
        id: row.id,
        state: 1
      }).then((res) => {
        if (res.data.state === '000') {
          this.$message.success('启用成功！')
          this.loading = false
          this.getVehicleData({
            page: this.page,
            pagesize: this.pagesize
          })
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    handleClose(row) {
      this.loading = true
      this.editVehicleData({
        id: row.id,
        state: 0
      }).then((res) => {
        if (res.data.state === '000') {
          this.$message.success('暂停成功！')
          this.loading = false
          this.getVehicleData({
            page: this.page,
            pagesize: this.pagesize
          })
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    handleCancelDialog() {
      this.visible = false
    },
    handleConfirmDialog(playload) {
      this.confirmData = playload
      this.visible = false
      this.confirmVisible = true
    },
    handleCancel() {
      this.confirmVisible = false
      this.visible = true
    },
    handleConfirm() {
      this.loading = true
      this.editVehicleData(this.confirmData).then((res) => {
        if (res.data.state === '000') {
          this.$message.success('编辑成功！')
          this.loading = false
          this.getVehicleData({
            page: this.page,
            rows: this.pagesize,
            ...this.selectData
          })
        } else {
          this.$message.error(res.date.msg)
        }
      })
      this.confirmVisible = false
    },
    handleClickPage(page) {
      this.loading = true
      this.page = page
      this.getVehicleData({
        page: page,
        rows: this.pagesize,
        ...this.selectData
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        } else {
          this.$message.error(res.state.msg)
        }
      })
    },
    handleSizeChange(pagesize) {
      this.loading = true
      this.pagesize = pagesize
      this.getVehicleData({
        page: this.page,
        rows: this.pagesize,
        ...this.selectData
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        } else {
          this.$message.error(res.state.msg)
        }
      })
    }
  }
}
</script>

